package com.kdtech.analyse.news;
import com.kdtech.analyse.AnalyseNews;
import com.kdtech.utils.HtmlCleaner;

import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;

import com.kdtech.crawler.CrawlHTML;
import com.kdtech.crawler.at.UrlArgumentTop;
import com.kdtech.entity.crawler.UrlMeta;
import com.kdtech.entity.data.NewsMeta;
import com.kdtech.utils.DateUtils;
import com.kdtech.utils.HtmlCleaner;
import com.kdtech.utils.StringUtils;
import com.kdtech.utils.HtmlCleaner;

public class CifnewsNewsAnalyse implements AnalyseNews {
	private static final String[] regex={
		"http://www.cifnews.com/Article/[0-9]+",
		"http://www.cifnews.com/.*/[0-9]+\\?origin=index_top_chu_[0-9]+",
	};


	
	public boolean isDetailPage(String url) {
		for (int i=0; i < regex.length; i++) {
			if (url.matches(regex[i])) {
				return true;
			}
		}
		return false;
	}

	
	public NewsMeta parserHtml(UrlMeta urlMeta) {
		NewsMeta news=new NewsMeta();
		String url=urlMeta.getUrl();
		/*
		 * 网站源码
		 */
		String htmltxt=urlMeta.getHtml();



		String title=null;
		String content=null;
		String author=null;
		Long date=null;
		Document doc=Jsoup.parse(htmltxt);

		/*
		 * 标题
		 */
		title=doc.select("div.article h1").text();


		/*
		 * 内容
		 */
		content =HtmlCleaner.getContentHtml(url , doc.select("div.article_con"));

		String text=doc.select("dl.article_icon").text();
		/*
		 * 作者
		 */
//		author=StringUtils.substringBetween(text, "来源：", " ");

		/*
		 * 时间
		 */
		date=DateUtils.matchDate(text);

		/*
		 * 链接
		 */
		news.setUrl(url);

		/*
		 * 来源
		 */
		news.setTitle(title);
		news.setContent(content);
		news.setAuthor(author);
		news.setDate(date);
		return news;
	}

	public static void main(String[] args) {
		CifnewsNewsAnalyse analyse=new CifnewsNewsAnalyse();
		String url="http://www.cifnews.com/article/22754?origin=index_top_chu_8";
		System.out.println(analyse.isDetailPage(url));
		url=UrlArgumentTop.FromatUrl(url);
		System.err.println(url);
		UrlMeta meta=CrawlHTML.responseToURL(url);
		System.out.println(analyse.parserHtml(meta));
	}

	
	public NewsMeta Update(NewsMeta meta) {
		return null;
	}


	
	public boolean isNeedUpdate(){
		return false;
	}

}
